package com.codeplex.springsurv.controllers;

import com.codeplex.springsurv.*;
import com.codeplex.springsurv.businessLogic.SurveyService;
import com.codeplex.springsurv.businessLogic.UserService;
import com.codeplex.springsurv.dataaccess.mappers.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.apache.ibatis.annotations.Param;

@Controller

public class SurveyController {
	
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	
	
	@Resource(name = "UserService")
	private UserService userService;
	
	@Resource(name = "SurveyService")
	private SurveyService surveyService;
	
	// Gets a specific survey by it's Id.
	@RequestMapping(value = "/surveys/{pId}", method = RequestMethod.GET)
	public Object getSurvById(@PathVariable(value = "pId") Long pId, HttpServletResponse response) throws IOException{
		return JsonView.Render(this.surveyService.getSurvById(pId), response);
	}
	
	// Registers a new survey in the system.
	// Receives as parameter a survey. Inserts the new survey in the
	// system.
	@RequestMapping(value = "/surveys/new", method = RequestMethod.POST)
	@Transactional(propagation = Propagation.REQUIRED)
	public void insertSurvey(@RequestBody final Survey pSurvey) throws IOException {
		try {
			this.surveyService.insertNewSurvey(pSurvey);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	// Changes the state of a survey to inactive.
	@RequestMapping(value = "/surveys/delete/{id}", method = RequestMethod.DELETE)
	public void closeSurvey(@PathVariable(value = "id")Long id) throws IOException {
		try{
			this.surveyService.closeSurvey(id);
		}catch (SQLException e){
			e.printStackTrace();
		}
	}
	
	// Receives as parameter an instance of UserSurvey containing the answers
	// chosen by the user who filled the survey, the identity of that survey
	// and the nickname of user who filled it.
	@RequestMapping(value = "/surveys/fill", method = RequestMethod.POST)
	@Transactional(propagation = Propagation.REQUIRED)
	public void insertFilledSurvey(@RequestBody final UserSurvey pUserSurvey) throws IOException{
		try{
			this.surveyService.insertFilledSurvey(pUserSurvey);
		}catch (SQLException e){
			e.printStackTrace();
		}
	}
	
	// Receives as parameter the Id of a given question and returns the percentage of users
	// that chose each answer.
	@RequestMapping(value = "/surveys/stats/{pQuestId}", method = RequestMethod.GET)
	public Object getStatsByQuestion(@PathVariable(value = "pQuestId") Long pQuestId, HttpServletResponse response) throws IOException, SQLException{
		return JsonView.Render(this.surveyService.getStatsByQuestId(pQuestId), response);
	}
	
	// Receives as parameter a User and gets a list of surveys not filled or
	// created by the user.
	@RequestMapping(value = "/surveys/notFilled", method = RequestMethod.POST)
	public Object getListSurveysNotFilledByUser(@RequestBody final User pUser, HttpServletResponse response) throws IOException, SQLException {
		return JsonView.Render(this.surveyService.getSurvLstNotFilledByUser(pUser), response);
	}
	
	// Receives as parameter an instance of User and gets a list of surveys created by the user.
		@RequestMapping(value = "/surveys/creator", method = RequestMethod.POST)
		public Object getSurveysListByUserCreator(@RequestBody final User pUser, HttpServletResponse response) throws IOException, SQLException {
			return JsonView.Render(this.surveyService.getSurvLstCreatedByUser(pUser), response);
		}
}
